package com.joolun.system.service.impl;

import com.joolun.common.constant.Constants;
import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.core.redis.RedisCache;
import com.joolun.common.exception.CustomException;
import com.joolun.common.utils.SecurityUtils;
import com.joolun.common.utils.StringUtils;
import com.joolun.common.utils.msg.MsgUtil;
import com.joolun.system.service.IAppService;
import com.joolun.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

/**
 * 用户 业务层处理
 *
 * @author chenlei
 */
@Service
public class AppServiceImpl implements IAppService {
    private static final Logger logger = LoggerFactory.getLogger(AppServiceImpl.class);

    private final String DAPP_WITHDRAW_ADDRESS = "http://127.0.0.1:9090/v1/bnb/withdraw";

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysUserService userService;


    @Override
    @Transactional
    public String getVerificationCode(String phone) {
        if (StringUtils.isEmpty(phone)) {
            throw new CustomException("手机号不能为空!");
        }
        MsgUtil sm = new MsgUtil();
        String verificationCode = sm.sendCustomMsg(phone, "");

        redisCache.setCacheObject(Constants.CAPTCHA_CODE_KEY + phone, verificationCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        return verificationCode;
    }

    @Override
    @Transactional
    public void setPhone(SysUser user, String newPhone) {
        String phone = "";
        // 原手机号为空向刚填的手机号发送验证码，否则向原来的手机号发送验证码
        if (user.getPhonenumber().isEmpty()) {
            phone = newPhone;
        } else {
            phone = user.getPhonenumber();
        }
        String verCode = redisCache.getCacheObject(Constants.CAPTCHA_CODE_KEY + phone);
        if (user.getVerCode().equalsIgnoreCase(verCode)) {
            user.setPhonenumber(newPhone);
            userService.updateAppUser(user);
        } else {
            throw new CustomException("验证码有误，请重新获取验证");
        }
    }

    @Override
    @Transactional
    public void changePassword(SysUser user) {
        String verCode = redisCache.getCacheObject(Constants.CAPTCHA_CODE_KEY + user.getPhonenumber());
        if (user.getVerCode().equalsIgnoreCase(verCode)) {
            SysUser temp = new SysUser();
            temp.setUserId(user.getUserId());
            temp.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
            userService.updateAppUser(temp);
        } else {
            throw new CustomException("验证码有误，请重新获取");
        }
    }

    private BigDecimal getChildPercent(String userCode, String childCode) {
        BigDecimal percent = new BigDecimal("1");
        int pLen = userCode.length();
        String childPartCode = childCode.substring(pLen + 1);
        int genCnt = childPartCode.split("\\.").length;
        int i = 1;
        while (i < genCnt) {
            percent = percent.multiply(new BigDecimal("0.5"));
            i++;
        }
        return percent;
    }

   /* public static void main(String[] args) {
        BigDecimal percent = new BigDecimal("1");
        int pLen = "0.2".length();
        String childPartCode = "0.2.1.1.1".substring(pLen + 1);
        int genCnt = childPartCode.split("\\.").length;
        int i = 1;
        while (i < genCnt) {
            percent = percent.multiply(new BigDecimal("0.5"));
            i++;
        }
        System.out.println(percent);
    }*/

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("20.00");
        BigDecimal b = new BigDecimal("2.00");

        System.out.println(new BigDecimal("20.00").multiply(new BigDecimal("2")).divide(new BigDecimal("100")));
    }


}
